import { group_findUsersById, dept_listAll, group_listAll } from '@/api/group'
import { user_listAll } from '@/api/user'
import { formateTree } from '@/utils/formate'; 

const group = {
  state: {
    group_user_list: [], //小组列表
    dept_user_list: [], //小组被选中的人员列表
    dept_child: [], //当前被选中部门的子集
    company_name: '', //公司名称
    cache_dept: {}, //当前被选中的部门对象 包含子集
    menu_dept_list: [], //部门列表菜单
    menu_group_list: [], //小组列表菜单
    cache_group: {},//当前被选中的小组对象
  },

  mutations: {
    SET_GROUP_USER_LIST: (state, list) => {
        state.group_user_list = list
    },
    SET_DEPT_USER_LIST: (state, list) => {
        state.dept_user_list = list
    },
    SET_DEPT_CHILD: (state, list) => {//设置子集
        state.dept_child = list
    },
    SET_COMPANY_NAME: (state, name) => {
        state.company_name = name
    },
    SET_CACHE_DEPT: (state, dept) => {
        state.cache_dept = dept
    },
    SET_MENU_DEPT_LIST: (state, list) => {
        state.menu_dept_list = list
    },
    SET_MENU_GROUP_LIST: (state, list) => {
        state.menu_group_list = list
    },
    SET_CACHE_GROUP: (state, group) => {
        state.cache_group = group
    }
    
  },

  actions: {
    // 用户名登录
    async FindUsersByGroupId({ commit }, group) {
        if (group) {
            let { data } = await group_findUsersById({id: group.id});
            if(data.status == 100){
                commit('SET_GROUP_USER_LIST', data.data.rows)
                commit('SET_CACHE_GROUP',group)
            }
        }
    },
    async FineUsersByDeptId({ commit }, id) {
        let { data } = await user_listAll({dept_id: id, company_id: localStorage.getItem('companyId')})
        if(data.status == 100){
            commit('SET_DEPT_USER_LIST', data.data.rows)
        }
    },
    async SetDeptChildList({ commit } , list) {//设置部门子集
        commit('SET_DEPT_CHILD', list)
    },
    async SetCompanyName({ commit } , name) {//设置公司名称
        commit('SET_COMPANY_NAME', name)
    },
    async SetCacheDept({ commit } , dept) {//设置公司名称
        commit('SET_CACHE_DEPT', dept)
    },
    async SetDeptList({ commit }) {//设置部门菜单
        let { data } = await dept_listAll({company_id : localStorage.getItem("companyId")});
        if(data.status == 100){
            commit('SET_MENU_DEPT_LIST', formateTree(data.data.rows))
            return data.data.rows
        }
    },
    async SetGroupList({ commit }) {//设置小组菜单
        let { data } = await group_listAll({company_id : localStorage.getItem("companyId")});
        if(data.status == 100){
            commit('SET_MENU_GROUP_LIST', data.data.rows)
            return data.data.rows;
        }
    },
    
  }
}

export default group
